<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

class CreateUsersTable extends Migration
{
	private $tableName = 'users';
	private $tableComment = '用户表';
	private $primaryKey = 'uid';

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::create($this->tableName, function (Blueprint $table) {
			$table->increments(env('DB_CONNECTION') == 'oracle' ? strtoupper($this->primaryKey) : $this->primaryKey)->comment('用户id');
			$table->string('username', 100)->comment('用户名')->unique();
			$table->string('nickname', 100)->comment('昵称')->nullable();
			$table->string('avatar', 500)->comment('头像')->nullable();
			$table->string('phone', 60)->comment('用户手机号')->nullable();
			$table->timestamp('last_login')->comment('最后登录时间')->nullable();
			$table->string('lastloginip', 15)->comment('最后登录IP')->nullable();
			$table->string('openid', 50)->comment('第三方id');
			$table->rememberToken();
			$table->char('api_token', 32)->comment('APP token')->nullable();
			$table->char('plat', 3)->comment('注册平台来源')->nullable();
			$table->unsigndTinyInteger('allow_share')->comment('是否允许分销，1允许，0不允许')->default(1);
			$table->unsignedTinyInteger('status')->comment('是否禁用账户，1正常，0禁用')->default(1);
			$table->Integer('money')->comment('佣金')->default(0);
			$table->timestamp('token_expired')->comment('token过期时间')->nullable();
			$table->timestamps();

			if (env('DB_CONNECTION') == 'oracle') {
				$table->comment = $this->tableComment;
			}
		});

		if (env('DB_CONNECTION') == 'mysql') {
			DB::statement("ALTER TABLE `" . DB::getTablePrefix() . $this->tableName . "` comment '{$this->tableComment}'");
		}
	}

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::dropIfExists($this->tableName);
		if (env('DB_CONNECTION') == 'oracle') {
			$sequence = DB::getSequence();
			$sequence->drop(strtoupper($this->tableName . '_' . $this->primaryKey . '_SEQ'));
		}
	}
}
